package com.fjd.controller;

import com.fjd.entity.Discount;
import com.fjd.result.Result;
import com.fjd.service.DiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shopping/discount")
@Slf4j
@Api(tags = "折扣管理")
public class DiscountController {
    @Autowired
    private DiscountService discountService;

    @GetMapping("/list")
    @ApiOperation("查询所有折扣")
    public Result<List<Discount>> list(){
        List<Discount> list = discountService.list();
        return Result.success(list);
    }

    @PostMapping("/add")
    @ApiOperation("新增商品折扣")
    public Result add(@RequestBody @Valid Discount discount){
        discountService.add(discount);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改商品折扣")
    public Result update(@RequestBody Discount discount){
        discountService.update(discount);
        return Result.success();
    }
    @PostMapping("/updateBatch")
    @ApiOperation("批量修改商品折扣")
    public Result updateBatch(@RequestBody List<Discount> discountList) {
        if (discountList == null || discountList.isEmpty()) {
            return Result.error("更新数据不能为空");
        }

        discountService.updateBatch(discountList);
        return Result.success();
    }

}
